import {Component, Inject, Optional} from '@angular/core';
import {GnDigestUtils, GnForm} from "geminis-ng";
import {MAT_DIALOG_DATA} from "@angular/material";

@Component({
  selector: 'gn-resources-user-edit',
  templateUrl: './user-edit.component.html'
})
export class UserEditComponent extends GnForm {

  forEdit: boolean;

  constructor(@Optional() @Inject(MAT_DIALOG_DATA) private param: any) {
    super();

    if (param.id != null) {
      this.forEdit = true;
    }
  }

  confirm(): void {
    if (!this.forEdit) {
      GnDigestUtils.sha256(this.form.formGroup.value.passwordValue)
        .then(value => {
          this.form.formGroup.value.password = {};
          this.form.formGroup.value.password.password = value;
          this.form.formGroup.value.passwordConfirm = null;
          this.form.formGroup.value.passwordValue = null;

          this.form.url = "/RESOURCES-SERVICE/users/" + this.param.orgId + "/属于";

          this.dialog.confirmForm();
        });
    }
  }

}
